package tk.mybatis.web.controller;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.*;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.web.bind.annotation.*;

import tk.mybatis.web.mapper.ModelTypeMapper;
import tk.mybatis.web.model.ModelType;

@RestController
@RequestMapping("api/myTest")
public class MyTest {
	@RequestMapping("list")
	public List<Integer> list(HttpServletRequest request, HttpServletResponse response, Integer from, Integer to) {
		List<Integer> list = new ArrayList<Integer>();
		if (from == null || to == null) {
			list.add(0);
			return list;
		}
		for (int i = from; i < to + 1; i++) {
			list.add(i);
		}
		return list;
	}
	@RequestMapping("data")
	public Map<String,String> data(@RequestParam Map<String,String> map)
	{
		//TODO: 测试
		return map;
	}
	
	@RequestMapping("sqliteTest")
	public String sqliteTest(HttpServletRequest request) throws Exception {
		String contextPath = request.getSession().getServletContext().getRealPath("");
		String driver = "org.sqlite.JDBC";
		String url = "jdbc:sqlite:"+contextPath+"\\ZXYY_CE_Arc_A_F1.db";
		PooledDataSource dataSource = new PooledDataSource();
		dataSource.setDriver(driver);
		dataSource.setUrl(url);
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource);
		Configuration configuration = new Configuration();
		factoryBean.setConfiguration(configuration);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		factoryBean.setMapperLocations(resolver.getResources("classpath:tk/mybatis/web/mapper/ModelTypeMapper.xml"));
		factoryBean.setTypeAliasesPackage("tk.mybatis.web.model");
		SqlSessionFactory factory = factoryBean.getObject();
		try (SqlSession session = factory.openSession()) {
			ModelTypeMapper mapper = session.getMapper(ModelTypeMapper.class);
			List<ModelType> list = mapper.selectAll();
			System.out.println(list);
		}
		return "aaa";
	}
}
